//
//  BCSEventBus.swift
//  BCEventBus
//
//  Created by Basic on 2019/6/12.
//  EventBus swift 适配


final public class BCSEventBus<T:Any> {
    
    /// 根据协议对象，发布某个OC协议的事件
    ///
    /// - Parameters:
    ///   - prop: 协议，必须继承 NSObjectProtocol
    ///   - handle: 具体需要执行协议的某个方法，发布者必须要知道发布协议的某个方法
    public class func pubProtocol(_ prop: Protocol, _  handle:((T) -> ())?) {
        BCEventBus<AnyObject>.share.publishProtocol(prop) { (target) in
            if let targetT = target as? T {
                handle?(targetT)
            }
        }
    }
    public class func pubProtocol( prop: Protocol, identifier:String?,  handle:((T) -> ())?) {
        BCEventBus<AnyObject>.share.publishProtocol(prop, withIdentifier: identifier) { (target) in
            if let targetT = target as? T {
                handle?(targetT)
            }
        }
    }
    /// 根据协议名称，发布某个协议的事件（该协议可以是oc协议，也可以是swift协议）
    ///
    /// - Parameters:
    ///   - handle: 具体需要执行协议的某个方法，发布者必须要知道发布协议的某个方法
    public class func pubProtocol(_ handle:((T) -> ())?) {
        BCEventBus<AnyObject>.share.publishProtocolName("\(T.self)") { (target) in
            if let targetT = target as? T {
                handle?(targetT)
            }
        }
    }
    public class func pubProtocol(identifier:String?, handle:((T) -> ())?) {
        BCEventBus<AnyObject>.share.publishProtocolName("\(T.self)", withIdentifier: identifier) { (target) in
            if let targetT = target as? T {
                handle?(targetT)
            }
        }
    }
}

//MARK: - class 事件
/// 订阅具体class事件
///
/// - Parameters:
///   - target: 需要订阅的target，如果target销毁，则监听的所有事件也销毁了
///   - eventCls: 需要订阅的事件 class
/// - Returns: return value description
public func BCSSubClass<CLASS>(_ target: AnyObject,_ eventCls: CLASS.Type) -> BCEvent<CLASS> where CLASS:AnyObject {
    return BCEventBus<AnyObject>.share.subscribeClass(CLASS.self, withIdentifier: nil, withTarget: target)  as! BCEvent<CLASS>
}
/// 订阅具体class事件，可以指定唯一标识符
///
/// - Parameters:
///   - target: 需要订阅的target，如果target销毁，则监听的所有事件也销毁了
///   - identifier: 唯一标识符，内部 class+identifier 作为事件标识符
///   - eventCls: 需要订阅的事件 class
/// - Returns: return value description
public func BCSSubClass<CLASS>(_ target: AnyObject, _ eventCls: CLASS.Type, _ identifier: AnyObject) -> BCEvent<CLASS> where CLASS:AnyObject {
    return BCEventBus<AnyObject>.share.subscribeClass(CLASS.self, withIdentifier: String(Unmanaged.passUnretained(identifier).toOpaque().hashValue), withTarget: target) as! BCEvent<CLASS>
}

/// 发布对象事件
///
/// - Parameters:
///   - eventCls: 事件的class，如果nil，则取event.class
///   - event: 事件对象
public func BCSPubClass<CLASS>(_ eventCls: CLASS.Type, _ event: Any?) -> () where CLASS:AnyObject {
    BCEventBus<AnyObject>.share.publishClass(CLASS.self, withIdentifier: nil, withEvent: event)
}
/// 发布对象事件
///
/// - Parameters:
///   - eventCls: 事件的class，如果nil，则取event.class
///   - identifier: 唯一标识符，内部 class+identifier 作为事件标识符
///   - event: 事件对象
public func BCSPubClass<CLASS>(_ eventCls: CLASS.Type, _ event: Any?, _ identifier:AnyObject) ->() where CLASS:AnyObject {
    BCEventBus<AnyObject>.share.publishClass(CLASS.self, withIdentifier: String(Unmanaged.passUnretained(identifier).toOpaque().hashValue), withEvent: event)
}

//MARK: - 字符串 事件
/// 订阅某个字符串事件
///
/// - Parameters:
///   - target: 需要订阅的target，如果target销毁，则监听的所有事件也销毁了
///   - eventName: 事件名字
///   - eventType: 事件类型
/// - Returns: return value description
public func BCSSubName<TYPE:Any>(_ target: AnyObject, _ eventName: String, _ eventType:TYPE) -> BCEvent<TYPE> {
    return BCEventBus<AnyObject>.share.subscribeName(eventName, withTarget: target) as! BCEvent<TYPE>
}
/// 订阅某个字符串事件
///
/// - Parameters:
///   - target: 需要订阅的target，如果target销毁，则监听的所有事件也销毁了
///   - eventName: 事件名字
/// - Returns: return value description
public func BCSSubName(_ target: AnyObject, _ eventName: String) -> BCEvent<AnyObject> {
    return BCEventBus<AnyObject>.share.subscribeName(eventName, withTarget: target)
}

/// 发布字符串事件
///
/// - Parameters:
///   - eventName: 事件名字
///   - data: 事件附带的数据
public func BCSPubName(_ eventName: String, _  data:AnyObject?) {
    BCEventBus<AnyObject>.share.publishName(eventName, withData: data)
}

//MARK: - 协议事件

/// 根据协议对象，订阅一组协议事件
///
/// - Parameters:
///   - target: 需要订阅的target，如果target销毁，则监听的所有事件也销毁了
///   - proto: 协议对象，必须继承NSObjectProtocol
public func BCSSubProtocol(_ target: AnyObject, _ proto:Protocol) {
    BCEventBus<AnyObject>.share.subscribeProtocol(proto, withTarget: target)
}
public func BCSSubProtocol(_ target: AnyObject, _ proto:Protocol, _ identifier:String?) {
    BCEventBus<AnyObject>.share.subscribeProtocol(proto, withIdentifier: identifier, withTarget: target)
}

/// 根据协议名称，订阅一组协议事件
///
/// - Parameters:
///   - target: 需要订阅的target，如果target销毁，则监听的所有事件也销毁了
///   - protoName: 协议名称，可以是oc协议，也可以是swift协议
public func BCSSubProtocolName(_ target: AnyObject, _ protoName:String) {
    BCEventBus<AnyObject>.share.subscribeProtocolName(protoName, withTarget: target)
}
public func BCSSubProtocolName(_ target: AnyObject, _ protoName:String, _ identifier:String?) {
    BCEventBus<AnyObject>.share.subscribeProtocolName(protoName, withIdentifier: identifier, withTarget: target)
}

//MARK: - 销毁事件
public func BCSEventDestory(_ target: AnyObject) {
    BCEventBus<AnyObject>.share.dispose(target)
}

